package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.DishDao;
import com.itheima.dao.DishFlavorDao;
import com.itheima.pojo.DTO.DishDto;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.IDishFlavorService;
import com.itheima.service.IDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements IDishService {

    @Autowired
    DishDao dishDao;
    @Autowired
    IDishFlavorService dishFlavorService;

    //新增菜品
    @Override
    public void saveDishWithFlavor(DishDto dishDto) {
        //1.先将数据放到Dish中
        dishDao.insert(dishDto);

        //2.判断用户是否选爱好
        //Mybatis-plus中insert一条记录之后将主键ID返回给实体对象
        //mybatis需要在xml文件中配置: xml文件里添加useGeneratedKeys=“true” keyProperty="id"

        Long id = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item)->{
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());

        //3.保存爱好
        dishFlavorService.saveBatch(flavors);
    }

    //回显数据
    @Override
    public DishDto getByIdWithFlavor(Long id) {

        //1.先查询是否有这个
        Dish dish = dishDao.selectById(id);

        DishDto dishDto = new DishDto();
        //2.复制到DishDto对象中
        BeanUtils.copyProperties(dish,dishDto);

        //3.查看是否有爱好
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId()); //说明表中存在该菜品的爱好

        List<DishFlavor> flavors = dishFlavorService.list(wrapper);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    //修改菜品
    @Override
    public void updateDishWithFlavor(DishDto dishDto) {
        //1.先修改dish里的数据
        dishDao.updateById(dishDto);

        Long id = dishDto.getId();
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,id);

        //先删除数据库中的爱好
        dishFlavorService.remove(wrapper);

        //2.查看是否选择了爱好
        List<DishFlavor> flavors = dishDto.getFlavors();
        //再添加到数据库
        flavors = flavors.stream().map((item)->{
            //设置id值
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());
        //保存爱好
        dishFlavorService.saveBatch(flavors);
    }

    /*
    * 批量删除
    * */
    @Override
    public void deleteDishById(Long[] ids) {
        //1.先查询数据库的菜品和菜品爱好
        for (Long id : ids) {
            //Dish dishById = dishDao.selectById(id);
            LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(wrapper);
            dishDao.deleteById(id);
        }
    }

    //起售、停售
    @Override
    public void updateStatus(Integer id, Long[] ids) {
        for (Long dishId : ids) {
            Dish dish = dishDao.selectById(dishId);
            dish.setStatus(id);
            dishDao.updateById(dish);
        }
    }
}
